/**
 * Class: SListeners
 * Array of listeners (callbacks). Every listener may define event name that it is
 * interested in.
 */
var Listeners = SMap.Class({
	init: function() {
		this.listeners = new Array();
	},
	/**
	 * Method: add
	 * Add new listener to array.
	 * 
	 * Propertyes: 
	 * eventName - string, name of event that this listener is interested in
	 * func - callback
	 */
	add: function(eventName, callback, me) {
		var func = function() { 
			return callback.apply(me, arguments) 
		};
		if (this.listeners[eventName]) {
			this.listeners[eventName].push(func);
		} else {
			this.listeners[eventName] = [func];
		};
	},
	/**
	 * Method: broadcast
	 * 
	 * Propertyes:
	 * eventName - call all listeners that are interested in this event. Function 
	 * can have any number of arguments. All of them, except first (eventName) will
	 * be transfered to callback function. 
	 */
	broadcast: function(eventName) {
		var result = null;
		
		if(this.listeners["*"]) {
			var l = this.listeners["*"];
			for(var i=0;i<l.length;i++) {
				result = l[i].apply(null, arguments);
			}
		}
		
		var args = [];
		for(var i=1;i<arguments.length;i++) {
			args.push(arguments[i]);
		}
		
		var l = this.listeners[eventName];
		if (l) for(var i=0;i<l.length;i++) {
			result = l[i].apply(null, args);
		}
		
		return result;
	},
	call: function() {
		return this.broadcast.apply(this, arguments);
	}
});
